package com.njtoyo.taxi.admin.rest.controller.open;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("open/file")
@Api(tags = {"文件管理器"})
public class FileController {

    @GetMapping("excel/{filename}")
    @ApiOperation(value = "下载 excel 模板", notes = "下载 excel 模板文件")
    public ResponseEntity<Resource> getExcelTemplate(@PathVariable("filename") String filename) {
        String attachment = ContentDisposition
                .builder("attachment")
                .filename(filename)
                .build().toString();
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, attachment)
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(new ClassPathResource("excel/" + filename));
    }
}
